package View;

import javax.swing.*;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import Model.*;
import Controller.*;

public class TelaCategoriaEstoque extends JScrollPane{
	static GerenciadorAplicacoes gapp = new GerenciadorAplicacoes();
	static JScrollPane categoria;
	static JButton cancelar, limpar, salvar;
	static JPanel cadastrar, cat;
	public static Categoria c = new Categoria();
	public JFormattedTextField cnome;
	JTextArea cdes;
	
	public TelaCategoriaEstoque() {
		categoria = new JScrollPane();
		cadastrar = new JPanel();
		categoria.setViewportView(cadastrar);
		categoria.getViewport().setBackground(new Color(100, 175, 239));
		cadastrar.setBackground(null);
		
		salvar = new JButton("SALVAR");
		salvar.setBackground(new Color(255, 255, 255));
		salvar.setForeground(new Color(0, 175, 239));
		salvar.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				salvar();
				TelaAplicacoes.AbrirEstoque();
			}});
		
		cancelar = new JButton("CANCELAR");
		cancelar.setBackground(new Color(255, 255, 255));
		cancelar.setForeground(new Color(0, 175, 239));
		cancelar.addActionListener(new ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				TelaAplicacoes.AbrirEstoque();
			}});
		
		
		limpar = new JButton("LIMPAR");
		limpar.setBackground(new Color(255, 255, 255));
		limpar.setForeground(new Color(0, 175, 239));
		limpar.addActionListener(new ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent evt) {
			cdes.setText("");
			cnome.setText("");
		}});
		
		cat = new JPanel();
		cat.setBackground(null);
		cat.setBorder(BorderFactory.createTitledBorder("CADASTRAR CATEGORIA"));
		
		JLabel lblNome = new JLabel("NOME");
		lblNome.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel lblDescrio = new JLabel("DESCRI\u00C7\u00C3O");
		lblDescrio.setHorizontalAlignment(SwingConstants.RIGHT);
		
		cnome = new JFormattedTextField();
		
		JScrollPane scrollPane = new JScrollPane();
		
		GroupLayout gl_cat = new GroupLayout(cat);
		
		gl_cat.setHorizontalGroup(
			gl_cat.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_cat.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_cat.createParallelGroup(Alignment.LEADING, false)
						.addComponent(lblNome, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblDescrio, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_cat.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
						.addComponent(cnome, GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE))
					.addContainerGap())
		);
		gl_cat.setVerticalGroup(
			gl_cat.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_cat.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_cat.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNome)
						.addComponent(cnome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_cat.createParallelGroup(Alignment.LEADING)
						.addComponent(lblDescrio)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE))
					.addGap(34))
		);
		
		cdes = new JTextArea();
		scrollPane.setViewportView(cdes);
		cat.setLayout(gl_cat);
		GroupLayout gl_cadastrar = new GroupLayout(cadastrar);
		gl_cadastrar.setHorizontalGroup(
			gl_cadastrar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_cadastrar.createSequentialGroup()
							.addComponent(salvar, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(cancelar, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(limpar, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE))
						.addComponent(cat, 0, 0, Short.MAX_VALUE))
					.addContainerGap(569, Short.MAX_VALUE))
		);
		gl_cadastrar.setVerticalGroup(
			gl_cadastrar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_cadastrar.createSequentialGroup()
					.addGap(5)
					.addComponent(cat, GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(gl_cadastrar.createParallelGroup(Alignment.BASELINE)
						.addComponent(salvar)
						.addComponent(cancelar)
						.addComponent(limpar))
					.addGap(237))
		);
		cadastrar.setLayout(gl_cadastrar);
	}

public Categoria getCategoria(){
	c.nome = cnome.getText();
	c.obs = cdes.getText();
	return c;
}

public void salvar(){
	gapp.cadastrarCategoria(getCategoria());

}

}
